import axios from 'axios'
import sha256 from 'js-sha256'

//翻译文本 - 有道第三方接口
const api = {
  form: 'auto',
  to: 'auto',
  appKey: '4b1f8f674280cd74',
  salt: 2,
  secret_key: 'KVXojbN0UC6NXfVG4ni1hQ2NOJ54UpDd',
  signType: 'v3'
}
function truncate(q){
  var len = q.length;
  if(len<=20) return q;
  return q.substring(0, 10) + len + q.substring(len-10, len);
}
export const translate = (str) => {
  let input=truncate(str)
  let nowTime = Math.round(new Date().getTime()/1000);
  const param = {
    q: str,
    form: api.form,
    to: api.to,
    appKey: api.appKey,
    salt: api.salt,
    curtime: nowTime,
    sign: sha256(api.appKey + input + api.salt + nowTime + api.secret_key),
    signType: api.signType
  };
  return axios.get('http://openapi.youdao.com/api', {params: param})
}
